import Common from "../Common";
import Tree from "./Tree";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Hall extends Common {

    @property({
        type: cc.Node,
        displayName: '水壶、点击则进行浇水动作'
    })
    protected shuihu

    @property({
        type: cc.Node,
        displayName: '肥料、点击则进行施肥动作'
    })
    protected feiliao

    @property({
        type: cc.Node,
        displayName: '除虫、点击则进行除虫动作'
    })
    protected chuchong


    @property({
        type: cc.Node,
        displayName: '兑换按钮、点击则前往商城'
    })
    protected duihuan

    protected toolbar: cc.Node

    @property(cc.Node)
    protected personal: cc.Node

    @property(cc.Node)
    protected huapen: cc.Node

    @property(cc.Node)
    protected taskDialog: cc.Node

    @property(cc.Node)
    protected playerWayDialog: cc.Node


    protected animateIsRunning = false

    protected watch = {
        userInfo: (val, oldVal) => {
            // 是否显示驱虫器
            const showChong = !val.deworming_task_finish || false
            this.chuchong.parent.active = showChong

            const account = val.account || {}
            cc.find('jinbi/amount', this.personal).getComponent(cc.Label).string = account.cash_account || '0.00'
            cc.find('yonghu/name', this.personal).getComponent(cc.Label).string = account.user_id
            cc.find('kuang2/name', this.huapen).getComponent(cc.Label).string = val.invest_level_name || ''
            this.attachTree(val.invest_level || 1, showChong)
        }

    }

    start() {
        this.toolbar = this.node.getChildByName('toolbar')
        this.initEmitter();
    }

    private async attachTree(level: number, showChong = false) {
        let treeNode = this.huapen.getChildByName('shu')
        if (treeNode) {
            let currentLevel = treeNode.getComponent(Tree).getLevel
            if (currentLevel == level) {
                treeNode.getComponent(Tree).showChong(showChong)
                return;
            }
            this.huapen.removeChild(treeNode);
        }
        const treePrefab = await this.getPrefab(`tree/shu${level}`)
        const tree = cc.instantiate(treePrefab)
        tree.name = 'shu';
        tree.getComponent(Tree).setLevel = level;
        tree.getComponent(Tree).showChong(showChong)
        this.huapen.addChild(tree)
    }

    /**
     * 事件监听
     */
    protected initEmitter() {
        this.toolbar.children.forEach((item: cc.Node) => {
            item.on(cc.Node.EventType.TOUCH_START, this.onToolbarItemClick.bind(this, item.name), this)
        })
        this.shuihu.on(cc.Node.EventType.TOUCH_START, this.onJiaoShui, this) // 点击水壶按钮
        this.feiliao.on(cc.Node.EventType.TOUCH_START, this.onShiFei, this) // 点击肥料包
        this.chuchong.on(cc.Node.EventType.TOUCH_START, this.onChuChong, this) // 点击除虫剂
    }

    private onToolbarItemClick(name) {
        if ([
            'shuihu_bg',
            'shifei_bg',
            'chuchong_bg',
        ].indexOf(name) > -1) {
            return;
        }
        this.onBtnClick()
        switch (name) {
            case 'duihuan': //兑换按钮
                cc.director.loadScene('shop')
                break;
            case 'tasks': //今日任务按钮
                this.node.getChildByName('tasks').active = true
                break;
            case 'chou': //抽奖按钮
                this.node.getChildByName('draw').active = true
                break;
            case 'wanfa': //玩法说明
                this.playerWayDialog.active = true
                break;
            case 'geren': //个人中心
                cc.director.loadScene('profile')
                break;
            case 'kaiqi':
                cc.director.loadScene('prizePool')
                break;
        }
    }

    /**
     * 浇水
     */
    private async onJiaoShui() {
        if (this.animateIsRunning) {
            return;
        }
        const shuNode = this.huapen.getChildByName('shu')
        shuNode.getComponent(Tree).onJiaoShui(this.shuihu, (running) => {
            this.animateIsRunning = running
        })

    }


    /**
     * 施肥
     */
    private async onShiFei() {
        if (this.animateIsRunning) {
            return;
        }
        const shuNode = this.huapen.getChildByName('shu')
        shuNode.getComponent(Tree).onShiFei(this.feiliao, (running) => {
            this.animateIsRunning = running
        })
    }

    /**
     * 除虫
     */
    private async onChuChong() {
        if (this.animateIsRunning) {
            return;
        }
        const shuNode = this.huapen.getChildByName('shu')
        shuNode.getComponent(Tree).onChuChong(this.chuchong, (running) => {
            this.animateIsRunning = running
            if (!running) {
                this.initUserInfo(true)
            }
        })
    }
}
